package com.ruoyi.hqfinance.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.InitTableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.hqfinance.domain.*;
import com.ruoyi.hqfinance.domain.vo.HqInformStandardSheetVo;
import com.ruoyi.hqfinance.domain.vo.HqInformStandardVo;
import com.ruoyi.hqfinance.service.HqInformStandardService;
import com.ruoyi.logistic.domain.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/hq/informStandard")
@Api(tags = "总部财务-网点通知达标统计")
public class HqInformStandardSheetController {
    @Autowired
    private HqInformStandardService hqInformStandardService;
    @Autowired
    private TableService tableService;

    @ApiOperation("通知明细")
    @PostMapping("/sheetCollect")
    public R sheetCollect(@RequestBody HqInformStandardSheetVo vo){
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null){
            return R.fail("请输入查询所需的必要条件");
        }
        List<HqInformStandardSheet> verifies = hqInformStandardService.sheetCollect(vo);
        if (verifies == null || verifies.size() == 0){
            return R.fail("暂无数据");
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",PageInfo.info(verifies));
            String[] fields = {"date", "region", "networkName", "contractedArea", "hqSolicitor", "receiptVolume"
                    , "callVolume", "callRate", "smsVolume", "smsRate", "callTaggedVolume", "callTaggedPerformedVolume",
                    "callTaggedPerformanceRate", "smsTaggedVolume", "smsTaggedNotificationVolume", "smsTaggedNotificationRate"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("sheetCollect");
            jsonObject.put("fields",tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }

    @ApiOperation("签收类型通知")
    @PostMapping("/collectSignType")
    public R collectSignType(@RequestBody HqInformStandardVo vo){
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getTimeType() == null){
            return R.fail("请输入查询所需的必要条件");
        }
        List<HqInformStandardSignType> signTypes = hqInformStandardService.collectSignType(vo);
        if (signTypes == null || signTypes.size() == 0){
            return R.fail("暂无数据");
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",signTypes);
            String[] fields = {"date", "networkName","receiptVolume"
                    , "callVolume", "callRate", "smsVolume", "smsRate"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("collectSignType");
            jsonObject.put("fields",tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }

    @ApiOperation("平台通知")
    @PostMapping("/collectPlatform")
    public R collectPlatform(@RequestBody HqInformStandardVo vo){
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getTimeType() == null){
            return R.fail("请输入查询所需的必要条件");
        }
        List<HqInformStandardPlatform> platforms = hqInformStandardService.collectPlatform(vo);
        if (platforms == null || platforms.size() == 0){
            return R.fail("暂无数据");
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",platforms);
            String[] fields = {"date", "networkName","smsRate"
                    , "callRate", "callTaggedPerformanceRate", "smsTaggedNotificationRate", ""};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("collectPlatform");
            jsonObject.put("fields",tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }

    @ApiOperation("末端通知")
    @PostMapping("collectEndInform")
    public R collect(@RequestBody HqInformStandardVo vo){
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getTimeType() == null){
            return R.fail("请输入查询所需的必要条件");
        }
        List<HqInformStandardEnd> ends = hqInformStandardService.endInform(vo);
        if (ends == null){
            return R.fail("暂无数据");
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",ends);
            String[] fields = {
                    "receiptVolume",
                    "callVolume",
                    "callRate",
                    "smsVolume",
                    "smsRate",
                    "smsSignVolume",
                    "callTaggedVolume",
                    "callTaggedPerformedVolume",
                    "smsTaggedNotificationRate",
                    "smsTaggedRate",
                    "smsTaggedNum",
                    "smsInformNum"
            };
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("collectEndInform");
            jsonObject.put("fields",tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }
}
